//
//  BrokenBlock.m
//  StarBugs
//
//  Created by ?? ? on 1/5/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Object/BrokenBlock.h"
#include "Util/ImageManager.h"
#include "Util/SoundManager.h"
#include "Util/ModuleController.h"
#include "Global.h"
#define MAX_HP 1

BrokenBlock::~BrokenBlock()
{

}

BrokenBlock::BrokenBlock()
{   
    mainSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ob_broken");
    addChild(mainSprite);
    objectType = objectType_broken;
    hp = MAX_HP;
}


void BrokenBlock::setBody(b2World *world)
{
    hp = MAX_HP;
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
	bodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO);
	bodyDef.userData = mainSprite;
	mainBody = world->CreateBody(&bodyDef);
	
    b2PolygonShape rec;
    rec.SetAsBox(20 / PTM_RATIO, 5 / PTM_RATIO);
	
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &rec;	
    fixtureDef.restitution = 0.5f;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void BrokenBlock::removeBody(b2World *_world)
{
    if(mainBody)
    {
        _world->DestroyBody(mainBody);
        mainBody = NULL;
        
    }
}

void BrokenBlock::addBody(int cnt, CCPoint p, b2World* _world)
{
    if(cnt % 2 == 0)
        particleSprite[cnt] = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ob_broken_p1");
    else
        particleSprite[cnt] = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ob_broken_p2");
    addChild(particleSprite[cnt]);
    particleSprite[cnt]->setPosition(p);
    
	b2BodyDef bodyDef;
	bodyDef.type = b2_dynamicBody;
    
#ifdef __ANDROID_
	bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
#endif
#ifdef __IOS_
    bodyDef.position.Set(p.x/PTM_RATIO/ModuleController::getImageScale(), p.y/PTM_RATIO/ModuleController::getImageScale());
#endif
	bodyDef.userData = particleSprite[cnt];
	particleBody[cnt] = _world->CreateBody(&bodyDef);
	
	// Define another box shape for our dynamic body.
    b2CircleShape circ;
    circ.m_radius = 5 / PTM_RATIO;
	// Define the dynamic body fixture.
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &circ;	
	fixtureDef.density = 0.1f;
//    fixtureDef.restitution = 0.5f;
//	fixtureDef.friction = 0.6f;
	particleBody[cnt]->CreateFixture(&fixtureDef);
    
    CCPoint pt;
    pt.x = CCRANDOM_MINUS1_1() * 0.3/3;
    pt.y = CCRANDOM_MINUS1_1() / 10;
    particleBody[cnt]->ApplyLinearImpulse(b2Vec2(pt.x, pt.y),
                                          b2Vec2(particleBody[cnt]->GetWorldCenter().x + CCRANDOM_MINUS1_1() * 0.05/3,
                                                 particleBody[cnt]->GetWorldCenter().y + CCRANDOM_MINUS1_1() * 0.05/3));
    particleBody[cnt]->ApplyTorque(1);
}

void BrokenBlock::crashBlock(b2World *_world)
{
    if(mainBody == NULL)
        return;
    
    hp -= 1;
    
    if(hp == 0)
    {
        SoundManager::PlaySoundEffect(sndEffect_iceBlock);
        _world->DestroyBody(mainBody);
        mainBody = NULL;
        mainSprite->setVisible(false);
        addBody(0, mainSprite->getPosition(), _world);
        addBody(1, mainSprite->getPosition(),  _world);
        addBody(2, mainSprite->getPosition(),  _world);
        addBody(3, mainSprite->getPosition(),  _world);
        addBody(4, mainSprite->getPosition(),  _world);
        addBody(5, mainSprite->getPosition(),  _world);
        
        scheduleTime = 0;
        schedule(schedule_selector(BrokenBlock::doBlock));
        worldTmp = _world;
    }
}


void BrokenBlock::doBlock(float dt)
{
    bool stop = false;
    scheduleTime += dt;
    
    for (b2Body* b = worldTmp->GetBodyList(); b; b = b->GetNext())
    {
        if(b == particleBody[0])
        {
            stop = true;
            break;
        }
    }
    
    if(stop == false)
    {
        unschedule(schedule_selector(BrokenBlock::doBlock));
        removeChild(particleSprite[0], false);
        removeChild(particleSprite[1], false);
        removeChild(particleSprite[2], false);
        removeChild(particleSprite[3], false);
        removeChild(particleSprite[4], false);
        removeChild(particleSprite[5], false);
//        removeAllChildrenWithCleanup(false);
        return;
    }
    
    for(int i = 0; i < 6; i++)
    {
        ((CCSprite*)particleBody[i]->GetUserData())->setOpacity(255 * (2.0 - scheduleTime)/2.0);
        if(scheduleTime > 2.0)
        {
            unschedule(schedule_selector(BrokenBlock::doBlock));
            removeChild((CCSprite*)particleBody[i]->GetUserData(), false);
            worldTmp->DestroyBody(particleBody[i]);
            particleBody[i] = NULL;
        }
    }
}

void BrokenBlock::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
        
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
}
